 $(document).ready(function() {
 	chatList();
 });
 
function chatList(){//请求聊天室列表
	$.ajax({
		type : "post",
		url : webHead+"/group/list",
		contentType: "application/x-www-form-urlencoded;charset=UTF-8",
		dataType : "json",
		data : {
		},
		success : function(data) {
			if(data.code==RESPONSE_OK){
				chatListData(data);
			}
		},
		error : function(XMLHttpRequest, textStatus, errorThrown) {
			alert("请求失败:这真是个悲伤的故事。");
		}
	});   
}

function chatListData(data){//解析聊天室列表数据
	var list = data.data;
	$("#chatList").empty();//清空
	var div = "";
	var num =0;
	for(var i=0;i<list.length;i++){
		num++;
		var chatId = list[i].groupId;
		var chatName = list[i].groupName;
		var userName = list[i].userName;
		if(chatName==""){
			chatName="这个房间没名字";
		}
		div+="<div class="+'"'+"titleDiv"+'"'+">"+
				"<span value = '"+chatId+"'  onclick='joinChat("+num+")' id='titleId"+num+"' class="+'"'+"titleSpan"+'"'+">"+chatName+"</span>"+
				"<span class="+'"'+"titleSpan"+'"'+">"+userName+"</span>"+
				"<input type='hidden' value="+chatId+" id='hiddenId"+num+"' />"+
			 "</div>";
	}
	$("#chatList").append(div);
}   

function joinChat(num){//加入
	//+"&chatName="+$("#titleId"+num).html()
	window.location.href="/"+appName+"/html/chat/chat.html?msgType="+MSG_TYPE_JOINGROUP+"&groupId="+$("#hiddenId"+num).val();
}
function createChat(){
	$.ajax({
	 	type : "post",
	 	url :  webHead+"group/check",
	 	contentType: "application/x-www-form-urlencoded;charset=UTF-8",
	 	dataType : "json",
	 	beforeSend: function (XMLHttpRequest) {         		
	 		XMLHttpRequest.setRequestHeader(HEAD_USER_ID, getCookie(COOKIE_USER_ID));             
	 	},
	 	success : function(data) {
	 		console.log(data);
	 		if(data.code==RESPONSE_OK){
	 			window.location.href="/"+appName+"/html/chat/chat.html?msgType="+MSG_TYPE_CREATEGROUP+"&groupId="+data.data.groupId;
	 		}else{	 			
	 			window.location.href="/"+appName+"/html/chat/createchat.html";
	 		}
	 	},
	 	error : function(XMLHttpRequest, textStatus, errorThrown) {
	 		alert("请求失败:这真是个悲伤的故事。");
	 	}
	 });
}